package service

import (
	"ruoyi-go/app/admin/model/monitor"
	"ruoyi-go/app/admin/model/tools"
	"ruoyi-go/pkg/mysql"
	"ruoyi-go/utils"
	"time"

	"github.com/jinzhu/copier"
)

var SysJobService = &sysJobService{}

type sysJobService struct{}

func (sysJobService) SelectJobList(params tools.SearchTableDataParam, isPage bool) ([]monitor.SysJob, int64) {
	var pageNum = params.PageNum
	var pageSize = params.PageSize
	sysJob := params.Other.(monitor.SysJob)

	var jobName = sysJob.JobName
	var jobGroup = sysJob.JobGroup
	var status = sysJob.Status
	var invokeTarget = sysJob.InvokeTarget

	var total int64
	db := mysql.MysqlDb().Model(monitor.SysJob{})

	if jobName != "" {
		db.Where("job_name like ?", "%"+jobName+"%")
	}
	if jobGroup != "" {
		db.Where("job_group = ?", jobGroup)
	}
	if status != "" {
		db.Where("status = ?", status)
	}
	if invokeTarget != "" {
		db.Where("invoke_target like concat('%', ?, '%')", invokeTarget)
	}

	if err := db.Count(&total).Error; err != nil {
		return nil, 0
	}

	var list []monitor.SysJob
	if isPage {
		offset := (pageNum - 1) * pageSize
		db.Order("job_id DESC").Offset(offset).Limit(pageSize).Find(&list)
	} else {
		db.Order("job_id DESC").Find(&list)
	}
	return list, total
}

func (sysJobService) FindJobById(jobId string) monitor.SysJob {
	var job monitor.SysJob
	err := mysql.MysqlDb().Where("job_id = ?", jobId).First(&job).Error
	if err != nil {
		panic(err.Error())
	}
	return job
}

func (sysJobService) SaveJob(jobParam monitor.SysJobParam, userId interface{}) {
	var job monitor.SysJob
	err := copier.Copy(&job, jobParam)
	if err != nil {
		panic(err.Error())
	}
	job.CreateTime = time.Now()
	user := SysUserService.FindUserById(userId)
	job.CreateBy = user.UserName
	err = mysql.MysqlDb().Model(&monitor.SysJob{}).Create(&job).Error
	if err != nil {
		panic(err.Error())
	}
}

func (sysJobService) UploadJob(jobParam monitor.SysJob, userId interface{}) {
	var job monitor.SysJob
	err := copier.Copy(&job, jobParam)
	if err != nil {
		panic(err.Error())
	}
	user := SysUserService.FindUserById(userId)
	job.UpdateBy = user.UserName
	job.UpdateTime = time.Now()
	err = mysql.MysqlDb().Save(&job).Error
	if err != nil {
		panic(err.Error())
	}
}

func (sysJobService) SelectJobLogList(params tools.SearchTableDataParam) ([]monitor.SysJobLog, int64) {
	var pageNum = params.PageNum
	var pageSize = params.PageSize
	sysJobLog := params.Other.(monitor.SysJobLog)
	var jobName = sysJobLog.JobName
	var jobGroup = sysJobLog.JobGroup
	var status = sysJobLog.Status

	var beginTime = params.Params.BeginTime
	var endTime = params.Params.EndTime

	var total int64
	db := mysql.MysqlDb().Model(monitor.SysJobLog{})

	if jobName != "" {
		db.Where("job_name like ?", "%"+jobName+"%")
	}
	if jobGroup != "" {
		db.Where("job_group = ?", jobGroup)
	}

	if status != "" {
		db.Where("status = ?", status)
	}
	if beginTime != "" {
		//Loc, _ := time.LoadLocation("Asia/Shanghai")
		//startTime1, _ := time.ParseInLocation("", beginTime, Loc)
		//endTime = endTime + " 23:59:59"
		//endTime1, _ := time.ParseInLocation(constants.TimeFormat, endTime, Loc)
		startTime1, endTime1 := utils.GetBeginAndEndTime(beginTime, endTime)
		//db.Where("create_time >= ? and create_time <= ?", startTime1, endTime1)
		db.Where("create_time >= ?", startTime1)
		db.Where("create_time <= ?", endTime1)
	}

	if err := db.Count(&total).Error; err != nil {
		return nil, 0
	}
	offset := (pageNum - 1) * pageSize
	var list []monitor.SysJobLog
	db.Order("job_log_id DESC").Offset(offset).Limit(pageSize).Find(&list)
	return list, total
}

func (sysJobService) FindJobLogById(id string) monitor.SysJobLog {
	var jobLog monitor.SysJobLog
	err := mysql.MysqlDb().Where("id = ?", id).First(&jobLog).Error
	if err != nil {
		panic(err.Error())
	}
	return jobLog
}

func (sysJobService) JobLogAdd(param *monitor.SysJobLog) {
	param.CreateTime = time.Now()
	if err := mysql.MysqlDb().Model(&monitor.SysJobLog{}).Create(&param).Error; err != nil {
		panic(err.Error())
	}
}

func (sysJobService) DetectJob(jobIds string) {
	if err := mysql.MysqlDb().Where("job_id in ( ? )", jobIds).Delete(&monitor.SysJob{}).Error; err != nil {
		panic(err.Error())
	}
}

func (sysJobService) ChangeStatus(jobId string, status string) {
	err := mysql.MysqlDb().Model(&monitor.SysJob{}).Where("job_id=?", jobId).Update("status", status).Error
	if err != nil {
		panic(err.Error())
	}
}

func (sysJobService) DetectJobLog(ids string) {
	err := mysql.MysqlDb().Where("id in ( ? )", utils.Split(ids)).Delete(&monitor.SysJobLog{}).Error
	if err != nil {
		panic(err.Error())
	}
}

func (sysJobService) ClearJobLog() {
	if err := mysql.MysqlDb().Exec("truncate table sys_job_log").Error; err != nil {
		panic(err.Error())
	}
}
